import Vue from 'vue';
import App from './App';
import router from './router';
import axios from 'axios';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';    // 默认主题
import '../static/css/icon.css';
import "babel-polyfill";
import 'viewerjs/dist/viewer.css'
import Viewer from 'v-viewer'
Vue.use(Viewer)
//配置项
Viewer.setDefaults({
  zIndexInline:9999
})

Vue.use(ElementUI, { size: 'small' });

Vue.mixin({
    methods:{
        isBlank:function(str){
            if(str == null || str == undefined){
                return true;
            }
            if(typeof(str) == 'string' && str.trim() == ''){
                return true;
            }
            return false;
        },
        isNotBlank:function(str){
            return !this.isBlank(str);
        },
        fMoney:function(row,column,money){
            if(money == null || money == undefined){
                return 0.00;
            }
            return money.toFixed(2);
        },
        fMinuteMoney:function(row,column,money){
            if(money == null || money == undefined){
                return 0.00;
            }
            return (money/100).toFixed(2);
        },
        fDate:function(row,column,cellValue){
            if(cellValue == null || cellValue == undefined || cellValue == ''){
                return '';
            }
            return new Date(cellValue).format('yyyy-MM-dd hh:mm:ss');
        },
        fYmdDate(row,column,cellValue){
            if(cellValue == null || cellValue == undefined || cellValue == ''){
                  return '';
            }
            return new Date(cellValue).format('yyyy-MM-dd');
        },
        toPage(url){
            this.$router.push(url);
        },
        isLevel2:function(){
            var val = getLogin();
            return val != null && val.parentId !== 0;
        },
        isLevel1:function(){
            var val = getLogin();
            return val != null && val.parentId === 0;
        },
        special:function(channel){
            var arr = ['cpsxs6','cpsxs7','cpsxs10','cpsxs37']
            return arr.filter(item => item === channel).length > 0;
        },
        getJSONArray(str){
            if(this.isBlank(str)){
                return [];
            }
            return JSON.parse(str);
        }
    }
})


axios.interceptors.request.use(request =>{
    // request.url = '/api'+request.url;
    request.headers.client = 'H5';
    request.headers.version = '1.0.0';
    return request;
});

axios.interceptors.response.use(response=>{
    if(response.data.code == '400'){ // 未登录
        router.push('/login');
        return Promise.reject(error.response.data)  // 返回接口返回的错误信息
    }
    return response;
},error=>{
    return Promise.reject(error.response.data)  // 返回接口返回的错误信息
});

Vue.prototype.$http = axios;
Vue.prototype.$api = axios;

//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
    if(navigator.userAgent.indexOf('MSIE') > -1){ // ID10一下浏览器不支持
    }
    const role = localStorage.getItem('userinfo');
    if(!role && to.path !== '/login'){
        next('/login');
    }else{
        next();
    }
});


window.getLogin = function(){
    const val = localStorage.getItem('userinfo');
    if(!val){
        return null;
    }
    try{
        return JSON.parse(val);
    }catch(e){}
    return null;
}

window.guid = function() {
  return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    return v.toString(16);
  });
}

Date.prototype.format = function(format) {
   var date = {
          "M+": this.getMonth() + 1,
          "d+": this.getDate(),
          "h+": this.getHours(),
          "m+": this.getMinutes(),
          "s+": this.getSeconds(),
          "q+": Math.floor((this.getMonth() + 3) / 3),
          "S+": this.getMilliseconds()
   };
   if (/(y+)/i.test(format)) {
          format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
   }
   for (var k in date) {
          if (new RegExp("(" + k + ")").test(format)) {
                 format = format.replace(RegExp.$1, RegExp.$1.length == 1
                        ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
          }
   }
   return format;
}
new Vue({
    router,
    render: h => h(App)
}).$mount('#app');
